package com.mall.shopping.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import com.mall.shopping.services.cache.CacheManager;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static com.mall.shopping.constant.GlobalConstants.PRODUCT_CATE_CACHE_KEY;
import static com.mall.shopping.constant.GlobalConstants.PRODUCT_CATE_EXPIRE_TIME;

/**
 * @program: cs-mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-21 20:54
 **/

@Slf4j
@Component
@Service
public class ProductCateServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ProductCateConverter productCateConverter;
    @Autowired
    CacheManager cacheManager;
    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse response = new AllProductCateResponse();
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (cacheManager.checkCache(PRODUCT_CATE_CACHE_KEY)==null) {
                Example example = new Example(ItemCat.class);
                example.setOrderByClause("sort_order" + " " + request.getSort());
                // 列举所有商品种类，并排序
                List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
                List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
                response.setProductCateDtoList(productCateDtos);
                // json
                String value = mapper.writeValueAsString(productCateDtos);
                cacheManager.setCache(PRODUCT_CATE_CACHE_KEY,value,PRODUCT_CATE_EXPIRE_TIME);
                System.out.println("从数据库中获取的"+productCateDtos.getClass().getName());
            }else {
                String cache = cacheManager.checkCache(PRODUCT_CATE_CACHE_KEY);
                TypeFactory typeFactory = mapper.getTypeFactory();
                CollectionType collectionType = typeFactory.constructCollectionType(List.class, ProductCateDto.class);

                List<ProductCateDto> productCateDtos = mapper.readValue(cache, collectionType);

                response.setProductCateDtoList(productCateDtos);
                System.out.println("从redis中获取的"+productCateDtos.getClass().getName());
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("ProductCateServiceImpl.getAllProductCate occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
